import Vue from "vue";
import VueI18n from "vue-i18n";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "assets/css/iconfont.css";
import en_element from "element-ui/lib/locale/lang/en";
import zh_element from "element-ui/lib/locale/lang/zh-CN";
import zh from "assets/js/lang/zh.js";
import en from "assets/js/lang/en.js";
import locale from "element-ui/lib/locale";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import VueAMap from "vue-amap";
import echarts from "echarts";
import refresh from 'components/common/refresh/refresh.vue'

Vue.prototype.$echarts = echarts;
Vue.component("refresh",refresh)

let num = 1;

let arr = ["/", "/login", "/index", "/404", "/payment"];
router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next();
  } else {
    let zd_sky = JSON.parse(localStorage.getItem("zd_sky"));
    let data = store.state.menu;
    if (!zd_sky && data.length < 1) {
      next({ path: "/login" });
    } else if (zd_sky && data.length < 1) {
      data = zd_sky.menu;
      if (data.length > 0 && num < 2) {
        num++;
        for (let item of data) {
          for (let child of item.children) {
            arr.push(child.index);
          }
        }
      }
      if (arr.indexOf(to.path) == -1) {
        next({ path: "/404" });
      } else {
        next();
      }
    } else if (!zd_sky && data.length > 0) {
      if (data.length > 0 && num < 2) {
        num++;
        for (let item of data) {
          for (let child of item.children) {
            arr.push(child.index);
          }
        }
      }
      if (arr.indexOf(to.path) == -1) {
        next({ path: "/404" });
      } else {
        next();
      }
    } else if (zd_sky && data.length > 0) {
      if (data.length > 0 && num < 2) {
        num++;
        for (let item of data) {
          for (let child of item.children) {
            arr.push(child.index);
          }
        }
      }
      
      if (arr.indexOf(to.path) == -1) {
        next({ path: "/404" });
      } else {
        next();
      }
    }
  }
});

Vue.use(VueAMap);
VueAMap.initAMapApiLoader({
  key: "31d3b30166ed6e2e56f0b3f3d98a4dd3",
  plugin: [
    "AMap.Scale",
    "AMap.OverView",
    "AMap.ToolBar",
    "AMap.MapType",
    "AMap.Geocoder",
  ],
  v: "1.4.4",
});

Vue.config.productionTip = false;
Vue.use(ElementUI);
Vue.use(VueI18n);

const messages = {
  en: {
    ...en,
    ...en_element,
  },
  zh: {
    ...zh,
    ...zh_element,
  },
};

const i18n = new VueI18n({
  locale: localStorage.getItem("lang") || "zh",
  messages: messages,
});

locale.i18n((key, value) => i18n.t(key, value));

new Vue({
  router,
  store,
  i18n,
  render: (h) => h(App),
}).$mount("#app");
